/*
 * Copyright (C) 2015 Daniel Uppenkamp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.maurersmarbles;

import com.google.common.base.Preconditions;
import com.googlecode.maurersmarbles.states.PopState;
import com.googlecode.maurersmarbles.states.PushState;
import com.googlecode.maurersmarbles.states.State;
import java.util.Stack;

/**
 *
 * @author Daniel Uppenkamp
 */
public class GameEngine {

    /**
     * The lock that is used to control access to the state stack.
     */
    protected static final Object STATE_STACK_LOCK = new Object();
    /**
     * The stack of states with the top state being the current state.
     */
    protected final Stack<State> stateStack = new Stack();

    /**
     * Handles the given input event based on the current state in the game.
     *
     * @param screen the screen
     * @param gameState the game state
     * @param inputEvent the input event
     */
    public void handleInputEvent(final Screen screen, final GameState gameState, final InputEvent inputEvent) {

        synchronized (STATE_STACK_LOCK) {

            if (!stateStack.isEmpty()) {

                // get the current state
                final State currentState = stateStack.peek();

                // create a new thread to run the state's handleInputEvent code
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {

                        State newState = currentState.handleInputEvent(screen, gameState, inputEvent);
                        if (newState != null) {
                            setState(screen, gameState, newState);
                        }

                    }

                });
                thread.start();

            }

        }

    }

    /**
     * Handles the resize event based on the current state in the game.
     *
     * @param screen the screen
     * @param gameState the game state
     * @param width the width
     * @param height the height
     */
    public void handleResizeEvent(final Screen screen, final GameState gameState, final int width, final int height) {

        synchronized (STATE_STACK_LOCK) {

            if (!stateStack.isEmpty()) {

                // get the current state
                final State currentState = stateStack.peek();

                // create a new thread to run the state's handleResizeEvent code
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {

                        currentState.handleResizeEvent(screen, gameState, width, height);

                    }

                });
                thread.start();

            }

        }

    }

    /**
     * Sets the state in the game.
     *
     * @param screen the screen
     * @param gameState the game state
     * @param state the state
     */
    public void setState(final Screen screen, final GameState gameState, State state) {

        Preconditions.checkNotNull(state, "Null state");

        synchronized (STATE_STACK_LOCK) {

            // update the stack of states
            if (state instanceof PushState) {
                stateStack.push(state);
            }
            else if (state instanceof PopState) {
                stateStack.pop();
            }
            else {
                if (!stateStack.isEmpty()) {
                    stateStack.pop();
                }
                stateStack.push(state);
            }

            // enter the new screen state
            if (!stateStack.isEmpty()) {

                /**
                 * Get the current state. We cannot simply use the state that we
                 * were given as parameter because it may have been a PopState.
                 */
                final State currentState = stateStack.peek();

                // create a new thread to run the state's enter code
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {

                        State newState = currentState.enter(screen, gameState);
                        if (newState != null) {
                            setState(screen, gameState, newState);
                        }

                    }

                });
                thread.start();

            }

        }

    }

}
